package cn.glutnn.subject.application.controller;

import cn.glutnn.subject.application.convert.SubjectLabelDTOConverter;
import cn.glutnn.subject.application.dto.SubjectLabelDTO;
import cn.glutnn.subject.common.entity.Result;
import cn.glutnn.subject.domain.enyity.SubjectLabelBO;
import cn.glutnn.subject.domain.service.SubjectLabelDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:   msh
 * Date:     2024/11/22 下午10:06
 * Description:
 */

@RestController
@RequestMapping("/label")
@Slf4j
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    // 添加标签
    @PostMapping
    public Result addLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController add subjectLabelDTO:{}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(), "标签名称不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getSortNum(), "排序号不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            subjectLabelDomainService.addLabel(subjectLabelBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLabelController add error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    // 修改标签
    @PutMapping
    public Result updateLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController update subjectLabelDTO:{}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLabelController update error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    // 删除标签
    @DeleteMapping
    public Result deleteLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController delete subjectLabelDTO:{}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            subjectLabelDomainService.removeById(subjectLabelBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLabelController delete error:{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    // 查询所有标签
    @GetMapping("/list")
    public Result<List<SubjectLabelDTO>> queryAllLabel() {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController queryAllLabel");
            }
            return Result.ok(SubjectLabelDTOConverter.INSTANCE.convertBoListToDtoList(subjectLabelDomainService.queryAllLabel()));
        } catch (Exception e) {
            log.error("SubjectLabelController queryAllLabel error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
       }
    }

    @GetMapping("/queryLabelBySubjectId")
    public Result<List<SubjectLabelDTO>> queryLabelBySubjectId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController queryLabelBySubjectId subjectLabelDTO:{}", subjectLabelDTO);
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToBo(subjectLabelDTO);
            return Result.ok(SubjectLabelDTOConverter.INSTANCE.convertBoListToDtoList(subjectLabelDomainService.queryLabelBySubjectId(subjectLabelBO)));
        } catch (Exception e) {
            log.error("SubjectLabelController queryLabelBySubjectId error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
